package com.hxh.generater.springBoot.simple.gen;

import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.util.GenUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.*;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater
 * @Author: HXH
 * @Description: 系统基础代码
 * @Date: 2023/11/28 15:08
 */
public class GenSimpleAddition{

    public static void generationRedisCacheClass(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "cache", "RedisCache");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\cache\\RedisCache");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void processRedisMaven(Project project) {
        String replaceWord = "<!--AUTO_INSERT_HERE-->";

        String redisDependency = "        <dependency>\n" +
                                 "            <groupId>org.springframework.boot</groupId>\n" +
                                 "            <artifactId>spring-boot-starter-data-redis</artifactId>\n" +
                                 "        </dependency>\n";
        try {
            File pomFile = GenUtil.newFile(project, "pom", "pom");
            FileWriter fileWriter = new FileWriter(pomFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "pom");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                    line = line.replaceAll("#ArtifactId#", project.getBackendProject().getProjectName());
                    line = line.replace(replaceWord,redisDependency);
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
